home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8944 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.bdsi.com!bdsi
  2. From: sonia_mangat@bdsi.com (Sonia Mangat)
  3. Newsgroups: comp.lang.c++
  4. Subject: Overloaded Virtual functions
  5. Date: Tue, 27 Feb 1996 10:34:46 GMT
  6. Organization: Beechwood Data Systems
  7. Message-ID: <4gv8to$7dm@news.bdsi.com>
  8. Reply-To: sonia_mangat@bdsi.com (Sonia Mangat)
  9. NNTP-Posting-Host: 204.107.74.107
  10.  
  11. Hi everybody,
  12. I have a question on overloaded virtual functions. I have included a
  13. program that has a parent class with overloaded virtual functions.
  14. I have also attached the compilation error I get.
  15.  
  16. The ways I can think of correcting this problem are
  17.  --> Declare the other prototype in the class B and make it call it's
  18.      parent's function.
  19.  --> Give different names for the virtual function.
  20.  
  21.  I don't like both these options. Is there any other way to solve this 
  22. problem?
  23.  
  24. Please respond to : vidhya@bdsi.com
  25.  
  26. Thanks in advance
  27. vidhya
  28.  
  29.  
  30. ***** Program: a.C ***********
  31.  
  32. 1: #include <iostream.h>
  33.  
  34. 2: class T
  35. 3: {
  36. 4:      public:
  37. 5: };
  38.  
  39.  
  40. 6: class A
  41. 7: {
  42. 8:      public:
  43. 9:              virtual void print(int)  { cout << "A int" << endl;}
  44. 10:             virtual void print(const T&) { cout << "A char" << endl; 
  45. }
  46. 11: };
  47.  
  48. 12: class B : public A
  49. 13: {
  50. 14:     public:
  51. 15:             virtual void print(int) { cout << "B int"  << endl;}
  52. 16: };
  53.  
  54.  
  55. 17: main()
  56. 18: {
  57.  
  58. 18: {
  59. 19:     T p;
  60. 20:     B b;
  61. 21:     b.print(p);
  62. 22:     return 0;
  63. 23: }
  64.  
  65.  
  66. ****** Compilation error ********
  67.  
  68. "a.C", line 20.30: 1540-293: (W) "B::print(int)" hides the virtual 
  69. function "A::
  70. print(const T&)".
  71. "a.C", line 28.17: 1540-055: (S) "T" cannot be converted to "int".
  72. "a.C", line 28.17: 1540-306: (I) The previous message applies to 
  73. argument 1 of function "B::print(int)".
  74.  
  75.